import uuid
from datetime import datetime

import pytz

from log import MySqlConnectPool


class InspectionGenAutoLogs:

    # 给一个全部参数的 init
    def __init__(self, id, city, template_name, status, params, create_time):
        self.id = id
        self.city = city
        self.template_name = template_name
        self.status = status
        self.params = params
        self.create_time = create_time

    @staticmethod
    def init_default(city, template_name, params):
        utc_time = datetime.now(pytz.utc)
        # 转换为北京时区
        beijing_time = utc_time.astimezone(pytz.timezone('Asia/Shanghai'))
        # 生成一个Id
        id = str(uuid.uuid4())
        # 创建并初始化 AutomaticDeployTask 实例
        instance = InspectionGenAutoLogs(id=id, city=city, template_name=template_name,
                                         status='待执行', params=params, create_time=beijing_time)
        instance.save_to_database()
        return id

    def __str__(self):
        return 'id = %s, template_name = %s, status = %s, params = %s, createTime = %s' % (
            self.id, self.template_name, self.status, self.params, self.create_time)

    @staticmethod
    def select_data_limit10():
        conn = MySqlConnectPool.pool.get_connection()
        cursor = conn.cursor()
        sql = (
            "select * from inspection_gen_auto_logs order by create_time desc limit 10")

        try:
            cursor.execute(sql)
            conn.commit()
            result = cursor.fetchall()
            # 将 result 组成对象，封装到 list 中
            result = [InspectionGenAutoLogs(*x) for x in result]
            return result
        except Exception as e:
            print("Error:", e)
            conn.rollback()
        finally:
            cursor.close()
            MySqlConnectPool.pool.return_connection(conn)

    def save_to_database(self):
        conn = MySqlConnectPool.pool.get_connection()
        cursor = conn.cursor()
        sql = (
            "insert into inspection_gen_auto_logs (id,city, template_name, status, params, create_time) "
            "values (%s, %s, %s, %s, %s, %s)")
        params = (
            self.id, self.city, self.template_name, self.status, self.params, self.create_time
        )

        try:
            cursor.execute(sql, params)
            conn.commit()
        except Exception as e:
            print("Error:", e)
            conn.rollback()
        finally:
            cursor.close()
            MySqlConnectPool.pool.return_connection(conn)

    @staticmethod
    def updateStatus(logId, status):
        conn = MySqlConnectPool.pool.get_connection()
        cursor = conn.cursor()
        sql = (
            "update inspection_gen_auto_logs set status = %s where id = %s")
        params = (
            status, logId
        )

        try:
            cursor.execute(sql, params)
            conn.commit()
        except Exception as e:
            print("Error:", e)
            conn.rollback()
        finally:
            cursor.close()
            MySqlConnectPool.pool.return_connection(conn)
